package com.foreknow.controller;

import com.foreknow.bean.Guest;
import com.foreknow.service.AdminService;
import com.foreknow.service.GuestService;
import com.foreknow.service.impl.AdminServiceImpl;
import com.foreknow.service.impl.GuestServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AddMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置从服务器端向客户端返回的内容的类型   MIME类型: text/html
        resp.setContentType("text/html;charset=UTF-8");
        //客户端向服务器端请求的编码格式（否则会产生乱码） 默认：IOS 8859-1
        req.setCharacterEncoding("UTF-8");
        //获取打印流用于向网页输出内容
        PrintWriter out = resp.getWriter();
        //获取到请求参数
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");

        Guest guest = new Guest();
        guest.setName(name);
        guest.setPhone(phone);
        guest.setEmail(email);
        guest.setTitle(title);
        guest.setContent(content);

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        guest.setCreatedTime(simpleDateFormat.format(date));

        GuestService service = new GuestServiceImpl();
        boolean isRight = service.save(guest);
        if(isRight) {
           req.getRequestDispatcher("getInfo").forward(req,resp);
        }else {
            out.println("error......");
        }
    }
}
